package com.cskaoyan.gateway.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author: 尹圣淼。
 * @Date: 2022/1/21 9:49
 */

@RestController
@RequestMapping("/user")
public class LoginController {
    @Reference(timeout = 300000, retries = 0, check = false)
    IKaptchaService kaptchaService;
    @Reference(timeout = 500000, retries = 0, check = false)
    ILoginService loginService;

    @PostMapping("/login")
    @Anoymous
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse servletResponse) {
        // map总共会接收到三个参数。
        // captcha，userName，userPwd

        String captcha = map.get("captcha");
        // 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        // 首先通过获得到的HttpRequest             拿到那个uuid
        // 在网页中网页前端生成的样式：kaptcha_uuid = .....
        String captchaUuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        // 将得到的uuid装入request中，
        // kaptchaRequest最终进入老师写的service中的方法，让老师写好的验证码service 帮我处理。
        kaptchaCodeRequest.setUuid(captchaUuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        // ----------------------验-证-码-已-判-断-结-束----------------------------------------------
        // 运行到这 即验证码正确后: 开始进入 我写的loginservice.login(登陆验证。)
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setPassword(userPwd);
        userLoginRequest.setUserName(userName);
        UserLoginResponse userLoginResponse = loginService.login(userLoginRequest);
        if (!userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            // 登录失败
            return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
        }

        // 登录成功后，要将response中的token拿出，
        // 将token存储到cookie中
        String jwtTokenStr = userLoginResponse.getToken();
        // 有效期为一小时
        Cookie cookie = CookieUtil.genCookie(TokenIntercepter.ACCESS_TOKEN, jwtTokenStr, "/", 3600);
        cookie.setHttpOnly(true);
        CookieUtil.setCookie(servletResponse, cookie);
        return new ResponseUtil<>().setData(userLoginResponse);
    }

    @GetMapping("/login")
    public ResponseData login(HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        if (userInfo == null) {
            return new ResponseUtil<>().setErrorMsg("token已失效");
        }
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
       // String userfile = object.get("file").toString();
        String username = object.get("username").toString();
        UserLoginDto userLoginDto = new UserLoginDto();
        userLoginDto.setUid(uid);
       // userLoginDto.setFile(userfile);
        userLoginDto.setUsername(username);
        return new ResponseUtil<>().setData(userLoginDto);
    }

    @GetMapping("/loginOut")
    public ResponseData logout(HttpServletResponse response){
        Cookie cookie = CookieUtil.genCookie("access_token", null, "/", 0);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return new ResponseUtil<>().setData(null);
    }

}